package newpackage;

import java.awt.Cursor;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Carlos Frias
 */
public class MainWindow extends javax.swing.JFrame {
    private Peer peerEnvia; 
    private PeerThread peerEscuta, peerEscutaDados;
    
    /** Creates new form MainWindow */
    public MainWindow() {
        super("P2P-SDIS");
        initComponents();
        this.setResizable(false);
        peerEscuta = new PeerThread(new Peer(), 1);
        peerEscuta.start();
        peerEscutaDados = new PeerThread(new Peer(), 3);
        peerEscutaDados.start();
        peerEnvia = new Peer();
        this.BotaoCancelarTransferencia.setEnabled(false);//fica para tratar depois
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ListaDeTransfericiasAtivas = new javax.swing.JList();
        ProgressBarTransferencias = new javax.swing.JProgressBar();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        AreaDeTexto = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        ListaDeFicheirosEncontrados = new javax.swing.JList();
        BotaoTransferir = new javax.swing.JButton();
        BotaoCancelarTransferencia = new javax.swing.JButton();
        TextFieldProcura = new javax.swing.JTextField();
        BotaoOkProcura = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        EndereçoIPMulticast = new javax.swing.JTextField();
        labelPortoDados = new javax.swing.JLabel();
        PortoDadosTextField = new javax.swing.JTextField();
        BotaoMudarIPPorto = new javax.swing.JButton();
        labelPortoControlo = new javax.swing.JLabel();
        PortoControloTextField = new javax.swing.JTextField();
        PastaPartilhaTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        BotaoMudarPastaPartilha = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        MenuPastaPartilha = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        ListaDeTransfericiasAtivas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ListaDeTransfericiasAtivasMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                ListaDeTransfericiasAtivasMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                ListaDeTransfericiasAtivasMouseExited(evt);
            }
        });
        jScrollPane1.setViewportView(ListaDeTransfericiasAtivas);

        jLabel1.setText("Transferências ativas:");

        AreaDeTexto.setColumns(20);
        AreaDeTexto.setEditable(false);
        AreaDeTexto.setRows(5);
        jScrollPane2.setViewportView(AreaDeTexto);

        jLabel2.setText("Informações gerais:");

        jLabel3.setText("Procurar ficheiros:");

        ListaDeFicheirosEncontrados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                ListaDeFicheirosEncontradosMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                ListaDeFicheirosEncontradosMouseExited(evt);
            }
        });
        jScrollPane3.setViewportView(ListaDeFicheirosEncontrados);

        BotaoTransferir.setText("Transferir");
        BotaoTransferir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                BotaoTransferirMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                BotaoTransferirMouseExited(evt);
            }
        });
        BotaoTransferir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoTransferirActionPerformed(evt);
            }
        });

        BotaoCancelarTransferencia.setText("Cancelar Transferência");
        BotaoCancelarTransferencia.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                BotaoCancelarTransferenciaMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                BotaoCancelarTransferenciaMouseExited(evt);
            }
        });

        TextFieldProcura.setBackground(new java.awt.Color(102, 255, 255));
        TextFieldProcura.setText("Pesquisar");
        TextFieldProcura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TextFieldProcuraActionPerformed(evt);
            }
        });

        BotaoOkProcura.setText("ok");
        BotaoOkProcura.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                BotaoOkProcuraMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                BotaoOkProcuraMouseExited(evt);
            }
        });
        BotaoOkProcura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoOkProcuraActionPerformed(evt);
            }
        });
        BotaoOkProcura.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                BotaoOkProcuraKeyPressed(evt);
            }
        });

        jLabel4.setText("Endereço IP multicast:");

        EndereçoIPMulticast.setText("224.0.2.10");

        labelPortoDados.setText("Porto dados:");

        PortoDadosTextField.setText("8966");

        BotaoMudarIPPorto.setText("Mudar");
        BotaoMudarIPPorto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                BotaoMudarIPPortoMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                BotaoMudarIPPortoMouseExited(evt);
            }
        });
        BotaoMudarIPPorto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoMudarIPPortoActionPerformed(evt);
            }
        });

        labelPortoControlo.setText("Porto controlo:");

        PortoControloTextField.setText("8967");
        PortoControloTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PortoControloTextFieldActionPerformed(evt);
            }
        });

        PastaPartilhaTextField.setText("d://p2p/Transferencias");
        PastaPartilhaTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PastaPartilhaTextFieldActionPerformed(evt);
            }
        });

        jLabel5.setText("Directório de partilha:");

        BotaoMudarPastaPartilha.setText("Mudar");
        BotaoMudarPastaPartilha.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                BotaoMudarPastaPartilhaMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                BotaoMudarPastaPartilhaMouseExited(evt);
            }
        });
        BotaoMudarPastaPartilha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotaoMudarPastaPartilhaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(ProgressBarTransferencias, javax.swing.GroupLayout.DEFAULT_SIZE, 633, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 633, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TextFieldProcura, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BotaoOkProcura))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 633, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 633, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(EndereçoIPMulticast, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelPortoDados)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(PortoDadosTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelPortoControlo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(PortoControloTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22)
                        .addComponent(BotaoMudarIPPorto))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(BotaoTransferir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BotaoCancelarTransferencia)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(PastaPartilhaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BotaoMudarPastaPartilha)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ProgressBarTransferencias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(TextFieldProcura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BotaoOkProcura))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BotaoTransferir)
                    .addComponent(BotaoCancelarTransferencia)
                    .addComponent(jLabel5)
                    .addComponent(PastaPartilhaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BotaoMudarPastaPartilha))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(EndereçoIPMulticast, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPortoDados)
                    .addComponent(PortoDadosTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPortoControlo)
                    .addComponent(PortoControloTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(BotaoMudarIPPorto)))
        );

        jMenu1.setText("Ficheiro");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0));
        jMenuItem1.setText("Sair");
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Opções");

        MenuPastaPartilha.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, 0));
        MenuPastaPartilha.setText("Pasta de partilha");
        jMenu2.add(MenuPastaPartilha);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("Ajuda");

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Acerca");
        jMenu3.add(jMenuItem2);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void TextFieldProcuraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TextFieldProcuraActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TextFieldProcuraActionPerformed

    private void BotaoOkProcuraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoOkProcuraActionPerformed
        
        //fazer a limpeza dos ficheiros encontrados em procuras anteriores
        peerEscuta.peer.foudsEncontrados.removeAllElements();
        peerEnvia.enviaProcura(TextFieldProcura.getText() + "\n");
        AreaDeTexto.setText("A procurar " + TextFieldProcura.getText());
        //Aguarda um pouco para puder receber os founds
        try {
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        Vector ficheirosEncontrados = this.peerEscuta.peer.foudsEncontrados;
        
        final String[] nomesFicheiros = new String[ficheirosEncontrados.size()];
        for(int i = 0; i < ficheirosEncontrados.size(); i++){
            nomesFicheiros[i]=((FoundInfo) ficheirosEncontrados.elementAt(i)).getFileShowInfo();
        }
        //A preencher a JList ListaDeFicheirosEncontrados de acordo com os ficheiros encontrados
        ListaDeFicheirosEncontrados.setModel(new javax.swing.AbstractListModel() {
            @Override
            public int getSize() { return nomesFicheiros.length; }
            @Override
            public Object getElementAt(int i) { return nomesFicheiros[i]; }
        });
        this.AreaDeTexto.setText("Selecione o(s) ficheiro(s) da lista em baixo e clique em Transferir.");
        
        
    }//GEN-LAST:event_BotaoOkProcuraActionPerformed
    /**
     * 
     * @param evt 
     */
    private void BotaoMudarIPPortoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoMudarIPPortoActionPerformed
        //TODO validar o IPAdress os portos de dados e de controlo
        int portoControlo = 8967,  portoDados = 8966;
        try {
            portoControlo = Integer.parseInt(this.PortoControloTextField.getText());
                if(portoControlo < 0 || portoControlo > 65535) {
                        throw new NumberFormatException();
                }
        }catch (NumberFormatException e) {
                this.AreaDeTexto.setText("ERRO: Porto inválido: " + this.PortoControloTextField.getText() 
                        + "\nPor favor introduza um porto válido [0-65535], de preferência superior a 1023");
        }
        try {
            portoDados = Integer.parseInt(this.PortoDadosTextField.getText());
                if(portoDados < 0 || portoDados > 65535) {
                        throw new NumberFormatException();
                }
        }catch (NumberFormatException e) {
                this.AreaDeTexto.setText("ERRO: Porto inválido: " + this.PortoDadosTextField.getText() 
                        + "\nPor favor introduza um porto válido [0-65535], de preferência superior a 1023");
        }
        peerEnvia = new Peer(portoControlo, portoDados, EndereçoIPMulticast.getText());
        this.AreaDeTexto.setText("Alterações efectuadas:\nEndereço Multicast: " 
                + EndereçoIPMulticast.getText() + "\nPorto de controlo: "
                + portoControlo + "\nPorto de dados: " + portoDados);
    }//GEN-LAST:event_BotaoMudarIPPortoActionPerformed

    private void PortoControloTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PortoControloTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_PortoControloTextFieldActionPerformed

    private void ListaDeTransfericiasAtivasMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ListaDeTransfericiasAtivasMouseEntered
        this.setCursor(Cursor.HAND_CURSOR);
    }//GEN-LAST:event_ListaDeTransfericiasAtivasMouseEntered

    private void ListaDeFicheirosEncontradosMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ListaDeFicheirosEncontradosMouseEntered
        this.setCursor(Cursor.HAND_CURSOR);
    }//GEN-LAST:event_ListaDeFicheirosEncontradosMouseEntered

    private void ListaDeTransfericiasAtivasMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ListaDeTransfericiasAtivasMouseExited
        this.setCursor(Cursor.DEFAULT_CURSOR);
    }//GEN-LAST:event_ListaDeTransfericiasAtivasMouseExited

    private void ListaDeFicheirosEncontradosMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ListaDeFicheirosEncontradosMouseExited
        this.setCursor(Cursor.DEFAULT_CURSOR);
    }//GEN-LAST:event_ListaDeFicheirosEncontradosMouseExited

    private void BotaoOkProcuraMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotaoOkProcuraMouseEntered
        this.setCursor(Cursor.HAND_CURSOR);
    }//GEN-LAST:event_BotaoOkProcuraMouseEntered

    private void BotaoOkProcuraMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotaoOkProcuraMouseExited
        this.setCursor(Cursor.DEFAULT_CURSOR);
    }//GEN-LAST:event_BotaoOkProcuraMouseExited

    private void BotaoTransferirMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotaoTransferirMouseEntered
        this.setCursor(Cursor.HAND_CURSOR);
    }//GEN-LAST:event_BotaoTransferirMouseEntered

    private void BotaoTransferirMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotaoTransferirMouseExited
        this.setCursor(Cursor.DEFAULT_CURSOR);
    }//GEN-LAST:event_BotaoTransferirMouseExited

    private void BotaoCancelarTransferenciaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotaoCancelarTransferenciaMouseEntered
        this.setCursor(Cursor.HAND_CURSOR);
    }//GEN-LAST:event_BotaoCancelarTransferenciaMouseEntered

    private void BotaoCancelarTransferenciaMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotaoCancelarTransferenciaMouseExited
        this.setCursor(Cursor.DEFAULT_CURSOR);
    }//GEN-LAST:event_BotaoCancelarTransferenciaMouseExited

    private void BotaoMudarIPPortoMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotaoMudarIPPortoMouseEntered
        this.setCursor(Cursor.HAND_CURSOR);
    }//GEN-LAST:event_BotaoMudarIPPortoMouseEntered

    private void BotaoMudarIPPortoMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotaoMudarIPPortoMouseExited
        this.setCursor(Cursor.DEFAULT_CURSOR);
    }//GEN-LAST:event_BotaoMudarIPPortoMouseExited

    private void BotaoOkProcuraKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_BotaoOkProcuraKeyPressed
        //int keycode = evt.getKeyCode();
        //if(keycode == KeyEvent.VK_ENTER) BotaoOkProcuraActionPerformed (null);
    }//GEN-LAST:event_BotaoOkProcuraKeyPressed

    private void BotaoMudarPastaPartilhaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotaoMudarPastaPartilhaMouseEntered
        this.setCursor(Cursor.HAND_CURSOR);
    }//GEN-LAST:event_BotaoMudarPastaPartilhaMouseEntered

    private void BotaoMudarPastaPartilhaMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotaoMudarPastaPartilhaMouseExited
        this.setCursor(Cursor.DEFAULT_CURSOR);
    }//GEN-LAST:event_BotaoMudarPastaPartilhaMouseExited

    private void BotaoMudarPastaPartilhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoMudarPastaPartilhaActionPerformed
        //TODO garantir que PastaPartilhaTextField.getText() é uma path válida
        this.peerEscuta.peer.dirTransferencias = this.PastaPartilhaTextField.getText();
        this.AreaDeTexto.setText("Alterações efectuadas:\nDirectório de partilha: " + this.PastaPartilhaTextField.getText());
    }//GEN-LAST:event_BotaoMudarPastaPartilhaActionPerformed

    private void PastaPartilhaTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PastaPartilhaTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_PastaPartilhaTextFieldActionPerformed

    private void BotaoTransferirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotaoTransferirActionPerformed
        final Object[] itemsSelecionados = ListaDeFicheirosEncontrados.getSelectedValues(); 
        int indices [] = ListaDeFicheirosEncontrados.getSelectedIndices();
        String info = "Selecionou para transferência os ficheiros:\n";
        for(int i = 0; i < itemsSelecionados.length; i++){  
            info += itemsSelecionados[i] + "\n";  
        }
        this.AreaDeTexto.setText(info);
        //A enviar um GET para cada um dos ficheiros selecionados
        for(int i = 0; i < indices.length; i++){
              PeerThread pt = new PeerThread(peerEnvia,4);
              pt.indice = i;
              pt.start();
        }
        //A preencher a JList ListaTransferênciasAtivas de acordo com os ficheiros selecionados
        this.ListaDeTransfericiasAtivas.setModel(new javax.swing.AbstractListModel() {
            @Override
            public int getSize() { return itemsSelecionados.length; }
            @Override
            public Object getElementAt(int i) { return itemsSelecionados[i]; }
        });
        //A limpar a JList ListaDeFicheirosEncontrados
        ListaDeFicheirosEncontrados.setModel(new javax.swing.AbstractListModel() {
            @Override
            public int getSize() { return 0; }
            @Override
            public Object getElementAt(int i) { return null; }
        });
        //TODO colocar a barra de progresso a evoluir ao em tempo real...(terá que ser usado Threads)
        if(indices.length > 0){
            double progresso = 0.0;
            while(progresso < 100.0){
                double soma = 0.0;
                for(int i = 0; i< indices.length; i++)
                    soma += peerEnvia.getPercentagem(i);
                progresso = soma /(indices.length);
                this.ProgressBarTransferencias.setValue((int) progresso);
            }
        }
        String s = "Transferência concluída de: ";
        for(int i = 0; i < itemsSelecionados.length; i++){  
            s += itemsSelecionados[i] + "\n";  
        }
        this.AreaDeTexto.setText(s);
    }//GEN-LAST:event_BotaoTransferirActionPerformed

    private void ListaDeTransfericiasAtivasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ListaDeTransfericiasAtivasMouseClicked
        // TODO add your handling code here:        
    }//GEN-LAST:event_ListaDeTransfericiasAtivasMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea AreaDeTexto;
    private javax.swing.JButton BotaoCancelarTransferencia;
    private javax.swing.JButton BotaoMudarIPPorto;
    private javax.swing.JButton BotaoMudarPastaPartilha;
    private javax.swing.JButton BotaoOkProcura;
    private javax.swing.JButton BotaoTransferir;
    private javax.swing.JTextField EndereçoIPMulticast;
    private javax.swing.JList ListaDeFicheirosEncontrados;
    private javax.swing.JList ListaDeTransfericiasAtivas;
    private javax.swing.JMenuItem MenuPastaPartilha;
    private javax.swing.JTextField PastaPartilhaTextField;
    private javax.swing.JTextField PortoControloTextField;
    private javax.swing.JTextField PortoDadosTextField;
    private javax.swing.JProgressBar ProgressBarTransferencias;
    private javax.swing.JTextField TextFieldProcura;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel labelPortoControlo;
    private javax.swing.JLabel labelPortoDados;
    // End of variables declaration//GEN-END:variables
}
